package com.genghis.ptas.question.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.question.dao.QuestionDao;
import com.genghis.ptas.question.entity.Answer;
import com.genghis.ptas.question.entity.Question;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Repository("questionDao")
public class QuestionDaoImpl extends PageDao implements QuestionDao {

    private StringBuilder getQuestionSql() {
        return new StringBuilder("SELECT * FROM question q WHERE 1=1");
    }

    private StringBuilder getAnswerSql() {
        return new StringBuilder("SELECT * FROM answer a WHERE 1=1");
    }

    @Override
    public Page<Question> findAllQuestionPageList(PageParameter pageParameter, Question question) {
        StringBuilder sql = getQuestionSql();
        if (!"".equals(question.getQuestionContent()) && null != question.getQuestionContent()) {
            sql.append(" AND q.question_content like '%").append(question.getQuestionContent()).append("%'");
        }
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(question), new BeanPropertyRowMapper<Question>(Question.class));
    }

    @Override
    public Page<Answer> findAllAnswerPageList(PageParameter pageParameter, Answer answer) {
        StringBuilder sql = getAnswerSql();
        if (!"".equals(answer.getAnswerContent()) && null != answer.getAnswerContent()) {
            sql.append(" AND a.answer_content like '%").append(answer.getAnswerContent()).append("%'");
        }
        sql.append(" AND a.question_id=").append(answer.getQuestionId());
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(answer), new BeanPropertyRowMapper<Answer>(Answer.class));
    }

    @Override
    public void addQuestion(Question question) {
        String sql = "INSERT INTO question (question_content, user_id, ask_date, state) " +
                " VALUES(:questionContent, :userId, :askDate, :state)";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(question));
    }

    @Override
    public void editQuestion(Question question) {
        String sql = "UPDATE question SET question_content = :questionContent, user_id = :userId, " +
                " ask_date = :askDate, state = :state, question_type = :questionType WHERE id = :id ";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(question));
    }

    @Override
    public Question findQuestionById(int id) {
        StringBuilder sql = getQuestionSql();
        sql.append(" AND q.id=?");
        return getJdbcTemplate().queryForObject(sql.toString(), new BeanPropertyRowMapper<Question>(Question.class), id);
    }

    @Override
    public void delAskByIds(String ids) {
        String sql = "DELETE FROM question WHERE id in(" + ids + ")";
        getJdbcTemplate().update(sql);
    }

    @Override
    public List<Question> findAllQuestionByContent(String content) {
        String sql = "SELECT * FROM question WHERE 1=1 ";
        if (content != null){
            sql += "AND question_content like '%"+ content +"%'";
        }
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Question>(Question.class));
    }
}
